package com.wjpeng.wms.web.controller;

import com.wjpeng.wms.domain.Brand;
import com.wjpeng.wms.query.OrderChartQueryObject;
import com.wjpeng.wms.query.QueryObject;
import com.wjpeng.wms.query.SaleChartQueryObject;
import com.wjpeng.wms.service.IBrandService;
import com.wjpeng.wms.service.IChartService;
import com.wjpeng.wms.service.IClientService;
import com.wjpeng.wms.service.ISupplierService;
import com.wjpeng.wms.util.Dictionary;
import com.wjpeng.wms.util.JSONResult;
import com.wjpeng.wms.util.JSONUtil;
import com.wjpeng.wms.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("chart")
public class ChartController {
    @Autowired
    private IChartService chartService;

    @Autowired
    private ISupplierService supplierService;

    @Autowired
    private IBrandService brandService;

    @Autowired
    private IClientService clientService;


    @RequestMapping("order")
    public String order(Model model, @ModelAttribute("qo") OrderChartQueryObject qo){
        model.addAttribute("result",chartService.queryOrderChart(qo));
        model.addAttribute("suppliers",supplierService.list());
        model.addAttribute("brands",brandService.list());
        return "chart/order";
    }


    @RequestMapping("sale")
    public String sale(Model model, @ModelAttribute("qo") SaleChartQueryObject qo){
        model.addAttribute("result",chartService.querySaleChart(qo));
        model.addAttribute("clients",clientService.list());
        model.addAttribute("brands",brandService.list());
        return "chart/sale";
    }


    @RequestMapping("saleByBar")
    public String saleBybar(Model model, @ModelAttribute("qo") SaleChartQueryObject qo){
        List<Object> x = new ArrayList<>();
        List<Object> y = new ArrayList<>();
        List<Map<String, Object>> maps = chartService.querySaleChart(qo);
        for (Map<String, Object> map : maps) {
            //取出当前行中的groupType和totalAmount两个值对应存入x/y
            Object groupType = map.get("groupType");
            Object totalAmount = map.get("totalAmount");
            x.add(groupType);
            y.add(totalAmount);
        }
        //转换成json存入模式对象中
        model.addAttribute("groupType", Dictionary.SALE_MAP.get(qo.getGroupType()));
        model.addAttribute("x", JSONUtil.toJSONString(x));//当前groupType转为json对象,然后共享数据
        model.addAttribute("y", JSONUtil.toJSONString(y));//当前totalAmount转为json对象,然后共享数据

        return "chart/saleByBar";

    }

    @RequestMapping("saleByPie")
    public String saleByPie(Model model, @ModelAttribute("qo") SaleChartQueryObject qo){
        List<Object> x = new ArrayList<>(); //["admin","bunny"]
        List<Object> datas = new ArrayList<>(); //[{name="admin",value=11},{name="bunny",value=12}]
        List<Map<String, Object>> maps = chartService.querySaleChart(qo);
        for (Map<String, Object> map : maps) {
            //取出当前行中的groupType和totalAmount两个值对应存入x/y
            Object groupType = map.get("groupType");//获取groupType的值
            x.add(groupType);//把groupType的值存入x

            Map<String,Object> obj = new HashMap<>();//一个键值对的对象
            obj.put("name",map.get("groupType"));//name="admin"
            obj.put("value",map.get("totalAmount"));//value=11
            datas.add(obj);//把该对象传入到datas
        }
        //转换成json存入模式对象中
        model.addAttribute("groupType", Dictionary.SALE_MAP.get(qo.getGroupType()));
        model.addAttribute("x", JSONUtil.toJSONString(x));//当前groupType转为json对象,然后共享数据
        model.addAttribute("datas", JSONUtil.toJSONString(datas));//当前totalAmount转为json对象,然后共享数据

        return "chart/saleByPie";

    }

}
